<?php

namespace app\admin\controller;

use app\admin\model\website\Users;
use app\admin\model\SystemQuick;
use app\common\controller\AdminController;
use broadcast\redis\setRedis;
use think\App;
use think\facade\Env;
use app\admin\model\SystemWebsite;

class Index extends AdminController
{

    /**
     * 后台主页
     * @return string
     * @throws \Exception
     */
    public function index()
    {
        // halt(11);
        // //获取网站信息  start
        // $website = setRedis::getCache(session("website"),'info');
        // if (empty($website)) {
        //     $website = SystemWebsite::where("website_ident",session("website"))->find();
        //     if ($website) $website = $website->toArray();
        //     setRedis::setCache(session("website"),"info",$website,0);
        // }
        // halt($website);
        //获取网站信息  end
        return $this->fetch('', [
            'admin' => session('admin'),
        ]);
    }
    /**
     * 后台欢迎页
     * @return string
     * @throws \Exception
     */
    public function welcome()
    {
        $quicks = SystemQuick::field('id,title,icon,href')
            ->where(['status' => 1])
            ->order('sort', 'desc')
            ->limit(8)
            ->select();
        $this->assign('quicks', $quicks);
        return $this->fetch();
    }

    /**
     * 修改管理员信息
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function editAdmin()
    {
        $id = session('admin.id');
        $row = (new Users())
            ->withoutField('password')
            ->find($id);
        empty($row) && $this->error('用户信息不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $this->isDemo && $this->error('演示环境下不允许修改');
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $row
                    ->allowField(['portrait', 'phone', 'email','lately_time'])
                    ->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }


            if ($save) {
                $info = setRedis::getUserCache(session("website"),'usersList#'.session('admin.id').':*');

                $userInfo = $info['data'];
                $userInfo['portrait'] = $post['portrait'];
                $userInfo['phone'] = $post['phone'];
                $key = $info['key'];
                $keyLsit = explode(":",$key);
                unset($keyLsit[0]);
                $nowKey = implode(":",$keyLsit);
                setRedis::setCache(session("website"),$nowKey,$userInfo,0);

                $this->success('保存成功');
            }else{
                $this->error('保存失败');
            }
        }
        $this->assign('row', $row);
        return $this->fetch();
    }

    /**
     * 修改密码
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function editPassword()
    {
        $id = session('admin.id');
        $row = (new Users())
            ->withoutField('password')
            ->find($id);
        if (!$row) {
            $this->error('用户信息不存在');
        }
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $this->isDemo && $this->error('演示环境下不允许修改');
            $rule = [
                'password|登录密码'       => 'require',
                'password_again|确认密码' => 'require',
            ];
            $this->validate($post, $rule);
            if ($post['password'] != $post['password_again']) {
                $this->error('两次密码输入不一致');
            }

            // 判断是否为演示站点
            // $example = Env::get('easyadmin.example', 0);
            // $example == 1 && $this->error('演示站点不允许修改密码');

            try {
                $save = $row->save([
                    'password' => md5($post['password']),
                ]);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            if ($save) {
                $info = setRedis::getUserCache(session("website"),'usersList#'.session('admin.id').':*');

                $userInfo = $info['data'];
                $userInfo['password'] = md5($post['password']);
                $key = $info['key'];
                $keyLsit = explode(":",$key);
                unset($keyLsit[0]);
                $nowKey = implode(":",$keyLsit);
                setRedis::setCache(session("website"),$nowKey,$userInfo,0);
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
        $this->assign('row', $row);
        return $this->fetch();
    }


    /**
     * 帮助
     */
    public function assist($type) {
        $defaultPath = "https://live.tanjfst.com";
        $imagePath = "";
        switch ($type) {
            case "room":
                $imagePath = $defaultPath."/assist/room.png";
                break;
            case "curriculum":
                $imagePath = $defaultPath."/assist/curriculum.png";
            break;
            case "lecturer":
                $imagePath = $defaultPath."/assist/lecturer.png";
            break;
            case "robot":
                $imagePath = $defaultPath."/assist/robot.png";
            break;
            case "statistics":
                $imagePath = $defaultPath."/assist/statistics.png";
            break;
            case "user":
                $imagePath = $defaultPath."/assist/user.png";
            break;
            case "system":
                $imagePath = $defaultPath."/assist/system.png";
            break;
        }
        $html = <<<HTML
        <img style="width:100%" src="{$imagePath}"/>
HTML;
        return $html;
    }

}
